//
//  BVMyDiscountViewController.m
//  BuyVegetables
//
//  Created by 叶思超 on 15/10/16.
//  Copyright (c) 2015年 叶思超. All rights reserved.
//

#import "BVMyDiscountViewController.h"
#import "BVConst.h"
#import "HttpTool.h"
#import "AppDelegate.h"
#import "BVMyDiscount.h"
#import "MJExtension.h"
#import "BVMyDiscountCell.h"
#import "BVLoginViewController.h"
#import "MJRefresh.h"
#import "MBProgressHUD+MJ.h"

@interface BVMyDiscountViewController () <UITableViewDataSource, UITableViewDelegate>
/**
 *  存放所有优惠券信息
 */
@property (nonatomic, strong) NSMutableArray *discountArray;
/**
 *  添加tableView
 */
@property (nonatomic, strong) UITableView *tableView;
// 当前页
@property (nonatomic, assign) NSInteger currentPage;
@end

@implementation BVMyDiscountViewController

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.frame = CGRectMake(0, 64, BVScreenW, BVScreenH - 64);
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.rowHeight = 140;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)discountArray {
    if (!_discountArray) {
        _discountArray = [NSMutableArray array];
    }
    return _discountArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化
    self.currentPage = 1;
    
    // 设置导航栏
    [self setupNav];
    
    // 加载我的优惠券
    [self loadMyDiscountData];
    
    [self configRefresh];
}
- (void)configRefresh{
    
    __weak typeof(self) ws = self;
    [self.tableView addHeaderWithCallback:^{
        ws.currentPage = 1;
        [ws loadMyDiscountData];
    }];
    
    
    [self.tableView addFooterWithCallback:^{
        ws.currentPage ++ ;
        [ws loadMyDiscountData];
    }];
}
#pragma mark -- 加载我的优惠券
- (void)loadMyDiscountData {
    
    // 拼接路径
    NSString *url = [NSString stringWithFormat:@"%@/VegetableUserAPI/getMyCouponList", Path];

    // 拼接参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *userinfo = [defaults dictionaryForKey:@"USERINFO"];
    NSString *user_id = userinfo[@"id"];
    if (user_id != nil) {
        params[@"user_id"] = user_id;
        params[@"page"] = @(self.currentPage);
        SHOWLOADING
        [HttpTool get:url params:params success:^(id responseObj) {
            HIDELOADING
            if ([responseObj[@"code"] isEqualToString:@"1"]) {
                
                if ([responseObj[@"code"] isEqualToString:@"1"]) {
                    if (self.currentPage == 1) {
                        [self.discountArray removeAllObjects];
                    }
                    
                    NSArray *array = [BVMyDiscount objectArrayWithKeyValuesArray:responseObj[@"data"]];
                    
                    [self.discountArray addObjectsFromArray:array];
                    
                    for (BVMyDiscount * discount in self.discountArray) {
                        discount.isAvailable = YES;
                    }
                    [self.tableView reloadData];
                }

            }
            if(self.discountArray.count == 0) {

                ERROR(@"没有可用抵价券");
            }else if(self.currentPage != 1){
                ERROR(@"没有更多优惠券了");
            }

            [self.tableView headerEndRefreshing];
            [self.tableView footerEndRefreshing];

        } failure:^(NSError *error) {
            [self.tableView headerEndRefreshing];
            [self.tableView footerEndRefreshing];
            NETWORKINGERROR
        }];
    }
}


#pragma mark -- UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.discountArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * view        = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 创建cell
    BVMyDiscountCell *cell = [BVMyDiscountCell cellWithTableView:tableView];

    // 传递模型
    cell.myDiscount = self.discountArray[indexPath.section];

    //不显示选择button

    cell.showChooseBtn  = NO;

    cell.selectionStyle  = UITableViewCellSelectionStyleNone;

    return cell;
}


/*
      |===============这是我的优惠券中，向左滑动删除该条信息，还有待完善======================|

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//左滑出现的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

//左滑删除cell
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [_discountArray removeObjectAtIndex:indexPath.row];
    
    //从列表中删除
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    
    
}
*/



- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 40;
}
#pragma mark -- 设置导航栏
- (void)setupNav {
    self.navigationController.navigationBar.barTintColor = BVBaseOrangeColor;
    self.title = @"我的优惠券";
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(backClick)];
}
#pragma mark -- 返回
- (void)backClick {

    ROOTNAVPOP
}
@end
